<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\goods\Category as GoodsCategory;
use app\common\model\goods\category\Region as CategoryRegion;
use app\common\model\goods\Goods as GoodsGoods;
use app\common\model\goods\Region;
use app\common\model\user\Info;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (商品分类)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="一级分类")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturnParams (name="type", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function categorys()
    {
        $params = request()->param();
        $type = request()->param('type', 0);
        extract($params);
        if ($type == 1) {
            $first_cate_ids = GoodsGoods::where('type', 2)->where('renewal', 0)->column('first_cate');
            $two_cate_ids = GoodsGoods::where('type', 2)->where('renewal', 0)->column('two_cate');
        } else {
            $first_cate_ids = Info::where('user_id', $this->auth->id)->column('first_cate');
            $two_cate_ids = Info::where('user_id', $this->auth->id)->column('two_cate');
        }

        $lists = GoodsCategory::all(['pid' => 0, 'id' => ['in', $first_cate_ids]], ['child' => function ($q) use ($two_cate_ids, $type) {
            $q->whereIn('id', $two_cate_ids);
        }]);
        $this->success('请求成功', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (商品分类（普通商品）)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $lists = GoodsCategory::all(['pid' => 0], ['child' => function ($q) {
            $q->with(['goods' => function ($data) {
                $data->where('type', 1);
                $data->where('status', 'normal');
                $data->order('weigh desc');
            }]);
        }]);
        $this->success('请求成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (商品列表)
     * @ApiMethod (GET)
     * @ApiParams (name="first_cate", type="integer", required=true, sample="", description="一级分类")
     * @ApiParams (name="two_cate", type="integer", required=true, sample="", description="二级分类")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="关键字")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $params = request()->param();
        extract($params);
        $model = new GoodsGoods;
        if (isset($first_cate) && $first_cate) {
            $model->where('first_cate', $first_cate);
        }
        if (isset($two_cate) && $two_cate) {
            $model->where('two_cate', $two_cate);
        }
        if (isset($keywords) && $keywords) {
            $model->whereLike('title|tags', '%' . $keywords . '%');
        }
        $model->where('type', '1');
        $model->where('status', 'normal');
        $model->order('weigh desc');
        $lists = $model->order('id desc')->paginate();
        $this->success('请求成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (商品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="区域ID(区域商品必传)" )
     * @ApiParams (name="goods_id", type="integer", required=true, sample="", description="商品id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $params = request()->param();
        extract($params);
        $goods = GoodsGoods::get($goods_id, ['spec']);
        if (isset($id) && $id) {
            $goods->region = Region::get($id);
        }
        $this->success('请求成功', $goods);
    }

    /**
     * @Author LK
     * @ApiTitle (区域商品列表)
     * @ApiMethod (GET)
     * @ApiParams (name="category_id", type="integer", required=true, sample="", description="二级分类ID")
     * @ApiParams (name="province", type="string", required=true, sample="", description="省(区域商品必传)")
     * @ApiParams (name="city", type="string", required=true, sample="", description="市(区域商品必传)")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function region_lists()
    {
        $params = request()->param();
        extract($params);
        $model = new GoodsGoods;
        if (!isset($category_id) || !$category_id) {
            $this->error('参数错误');
        }
        $cate = new CategoryRegion;

        if (isset($province) && $province) {
            $cate->where('province', $province);
        }
        if (isset($city) && $city) {
            $cate->where('province', $province);
            $cate->where('city', $city);
        }
        $r_ids =  $cate->where('category_id', $category_id)->column('id');
        $model->whereIn('region_id', $r_ids);
        $model->where('type', '2');
        $model->where('status', 'normal');
        $model->where('renewal', '0');
        $lists = $model->with(['region'])->order('id asc')->paginate();
        foreach ($lists as $v) {
            $v->pay_btn = true;
            $infoCount = Info::where([
                'region_id' => $v->region->id,
            ])->count();
            if ($infoCount >=  $v->region->num) {
                $v->pay_btn = false;
            }
        }
        $this->success('请求成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (区域商品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="goods_id", type="integer", required=true, sample="", description="商品id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function region_info()
    {
        $params = request()->param();
        extract($params);
        $goods = GoodsGoods::get($goods_id, ['region']);
        $goods->pay_btn = true;
        $infoCount = Info::where([
            'region_id' => $goods->region->id,
        ])->count();
        if ($infoCount >=  $goods->region->num) {
            $goods->pay_btn = false;
        }
        $this->success('请求成功', $goods);
    }
}
